package beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-07-07
 * Time:15:37
 */
//Controller控制器注解,控制层（做前端参数的校验）
@Controller
public class UserController {
    //    @Autowired
//    private User user1;
    @Autowired
    @Qualifier(value = "user2")
    private User user;
    public void func(){
        System.out.println("user:" + user);;
    }
//    @Resource(name = "user1")
//    private User user;
//    public void func(){
//        System.out.println("user:" + user);;
//    }

    //属性注入，使用Autowired
    @Autowired // 前提条件是UserRepository对象已经存储到Spring中了
    private UserRepository userRepository;
    public void func1(){
        userRepository.func();
    }
    private UserService userService;
    @Autowired  //构造方法注入，要遵守单一原则，多个参数是会注入失败的
    public UserController(UserService userService){
        this.userService = userService;
    }
    public UserController(UserService userService,int num){
        this.userService = userService;
    }
    public void func4(){
//        System.out.println("你好，controller");
        userService.func();
    }
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public void func2(){
//        System.out.println("你好，controller");
        userService.func();
    }
}
